package com.fs.mjx.service.impl;

import com.fs.mjx.Dao.RoleDao;
import com.fs.mjx.pojo.Role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;


public class RoleServiceImplTest {
    private RoleServiceImpl roleService;
    private SqlSession sqlSession;

    @Before
    public void setUp() throws Exception {
        //通过SqlSessionFactoryBuilder读取mybatis基础配置文件来构建sqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        try(InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")){
            SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
            //使用sqlSessionFactory打开数据库会话(true表示开启自动提交，即关闭事务机制)
            sqlSession = sqlSessionFactory.openSession(true);
            //使用sqlSession获取数据库操作UserDao的实现对象
            RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
            //创建一个RoleService
            roleService = new RoleServiceImpl(roleDao);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void getAllRole() {
        Set<Role> roleSet = roleService.getAllRole();
        System.out.println(roleSet);

    }
}